package com.snow.useviewdemo.function.surface;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;

import com.aliyun.player.AliPlayer;
import com.snow.common.tool.utils.MeasureUtils;


/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019/3/5
 * desc   :
 */

public class LivePlayView extends RelativeLayout {
    //视频画面
    private SurfaceView mSurfaceView;
    private int screenWidth;
    private int screenHeight;
    private Context mContext;
    public boolean isCanMove = true;

    public LivePlayView(Context context) {
        this(context, null);
    }

    public LivePlayView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LivePlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        screenWidth = MeasureUtils.init().getPhoneScreenWidth((Activity) mContext);
        screenHeight = MeasureUtils.init().getPhoneScreenHeight((Activity) mContext);
        //初始化播放用的surfaceView
        initSurfaceView();
    }

    private void initSurfaceView() {
        mSurfaceView = new SurfaceView(getContext().getApplicationContext());
        addSubView(mSurfaceView);

    }


    public void setLivePlayer(AliPlayer mAliyunVodPlayer) {
        SurfaceHolder holder = mSurfaceView.getHolder();
        //增加surfaceView的监听
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mAliyunVodPlayer.setDisplay(surfaceHolder);
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width,
                                       int height) {
                mAliyunVodPlayer.redraw();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            }
        });
        //按照布局撑满
//        mAliyunVodPlayer.setVideoScalingMode(IAliyunVodPlayer.VideoScalingMode.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        mAliyunVodPlayer.setDisplay(mSurfaceView.getHolder());

        if (isCanMove) {
            mSurfaceView.setZOrderOnTop(true);
        }
    }

    public void setZOrderOnTop() {
        mSurfaceView.setZOrderOnTop(true);
    }

    @Override
    public void setVisibility(int visibility) {
        if (visibility == GONE || visibility == INVISIBLE) {
            if (mSurfaceView != null) {
                mSurfaceView.setVisibility(GONE);
            }
        } else {
            if (mSurfaceView != null) {
                mSurfaceView.setVisibility(VISIBLE);
            }
        }
        super.setVisibility(visibility);
    }

    /**
     * addSubView
     * 添加子view到布局中
     *
     * @param view 子view
     */
    private void addSubView(View view) {
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(view, params);//添加到布局中
    }

    private int lastX;
    private int lastY;

    // 视图坐标方式
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isCanMove) {
            return super.onTouchEvent(event);
        }
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 记录触摸点坐标
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                // 计算偏移量
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                int left = getLeft() + offsetX;
                int top = getTop() + offsetY;
                int right = getRight() + offsetX;
                int bottom = getBottom() + offsetY;
                if (left <= 1) {//不要设置0，会一点一点吃掉的
                    left = 1;
                    right = left + getWidth();
                }
                if (top <= 1) {
                    top = 1;
                    bottom = top + getHeight();
                }
                if (right > screenWidth - 1) {
                    right = screenWidth;
                    left = screenWidth - getWidth();
                }
                if (bottom > screenHeight - 1) {
                    bottom = screenHeight;
                    top = screenHeight - getHeight();
                }
                layout(left, top, right, bottom);
                break;
        }
        return true;
    }
}
